home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / demos / trispd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-12  |  4.2 KB  |  204 lines

  1. /* trispd.c */
  2.  
  3. /*
  4.  * Simple GLUT program to measure triangle strip rendering speed.
  5.  * Brian Paul  February 15, 1997
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13. #include <sys/times.h>
  14. #include <sys/param.h>
  15.  
  16.  
  17. /*
  18.  * Return system time in seconds.
  19.  * NOTE:  this implementation may not be very portable!
  20.  */
  21. GLdouble GetTime( void )
  22. {
  23.    static GLdouble prev_time = 0.0;
  24.    static GLdouble time;
  25.    struct tms tm;
  26.    clock_t clk;
  27.  
  28.    clk = times(&tm);
  29.  
  30. #ifdef CLK_TCK
  31.    time = (double)clk / (double)CLK_TCK;
  32. #else
  33.    time = (double)clk / (double)HZ;
  34. #endif
  35.  
  36.    if (time>prev_time) {
  37.       prev_time = time;
  38.       return time;
  39.    }
  40.    else {
  41.       return prev_time;
  42.    }
  43. }
  44.  
  45.  
  46. static float MinPeriod = 2.0;   /* 2 seconds */
  47. static float Width = 400.0;
  48. static float Height = 400.0;
  49. static int Loops = 1;
  50. static int Size = 50;
  51.  
  52.  
  53.  
  54. static void Idle( void )
  55. {
  56.    glutPostRedisplay();
  57. }
  58.  
  59.  
  60. static void Display( void )
  61. {
  62.    float x, y;
  63.    float xStep;
  64.    float yStep;
  65.    double t0, t1;
  66.    double triRate;
  67.    double pixelRate;
  68.    int triCount;
  69.    int i;
  70.    float red[3] = { 1.0, 0.0, 0.0 };
  71.    float blue[3] = { 0.0, 0.0, 1.0 };
  72.  
  73.    xStep = yStep = sqrt( 2.0 * Size );
  74.  
  75.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  76.  
  77.    triCount = 0;
  78.    t0 = GetTime();
  79.    for (i=0; i<Loops; i++) {
  80.       for (y=1.0; y<Height-yStep; y+=yStep) {
  81.          glBegin(GL_TRIANGLE_STRIP);
  82.          for (x=1.0; x<Width; x+=xStep) {
  83.             glColor3fv(red);
  84.             glVertex2f(x, y);
  85.             glColor3fv(blue);
  86.             glVertex2f(x, y+yStep);
  87.             triCount += 2;
  88.          }
  89.          glEnd();
  90.          triCount -= 2;
  91.       }
  92.    }
  93.    t1 = GetTime();
  94.  
  95.    if (t1-t0 < MinPeriod) {
  96.       /* Next time draw more triangles to get longer elapsed time */
  97.       Loops *= 2;
  98.       return;
  99.    }
  100.  
  101.    triRate = triCount / (t1-t0);
  102.    pixelRate = triRate * Size;
  103.    printf("Rate: %d tri in %gs = %g tri/s  %d pixels/s\n",
  104.           triCount, t1-t0, triRate, (int)pixelRate);
  105.  
  106.    glutSwapBuffers();
  107. }
  108.  
  109.  
  110. static void Reshape( int width, int height )
  111. {
  112.    Width = width;
  113.    Height = height;
  114.    glViewport( 0, 0, width, height );
  115.    glMatrixMode( GL_PROJECTION );
  116.    glLoadIdentity();
  117.    glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
  118.    glMatrixMode( GL_MODELVIEW );
  119.    glLoadIdentity();
  120. }
  121.  
  122.  
  123. static void Key( unsigned char key, int x, int y )
  124. {
  125.    switch (key) {
  126.       case 27:
  127.          exit(0);
  128.          break;
  129.    }
  130.    glutPostRedisplay();
  131. }
  132.  
  133.  
  134. static void Init( int argc, char *argv[] )
  135. {
  136.    GLint shade;
  137.  
  138.    int i;
  139.    for (i=1; i<argc; i++) {
  140.       if (strcmp(argv[i],"-dither")==0)
  141.          glDisable(GL_DITHER);
  142.       else if (strcmp(argv[i],"+dither")==0)
  143.          glEnable(GL_DITHER);
  144.       else if (strcmp(argv[i],"+smooth")==0)
  145.          glShadeModel(GL_SMOOTH);
  146.       else if (strcmp(argv[i],"+flat")==0)
  147.          glShadeModel(GL_FLAT);
  148.       else if (strcmp(argv[i],"+depth")==0)
  149.          glEnable(GL_DEPTH_TEST);
  150.       else if (strcmp(argv[i],"-depth")==0)
  151.          glDisable(GL_DEPTH_TEST);
  152.       else if (strcmp(argv[i],"-size")==0) {
  153.          Size = atoi(argv[i+1]);
  154.          i++;
  155.       }
  156.       else
  157.          printf("Unknown option: %s\n", argv[i]);
  158.    }
  159.  
  160.    glGetIntegerv(GL_SHADE_MODEL, &shade);
  161.  
  162.    printf("Dither: %s\n", glIsEnabled(GL_DITHER) ? "on" : "off");
  163.    printf("ShadeModel: %s\n", (shade==GL_FLAT) ? "flat" : "smooth");
  164.    printf("Depth test: %s\n", glIsEnabled(GL_DEPTH_TEST) ? "on" : "off");
  165.    printf("Size: %d pixels\n", Size);
  166. }
  167.  
  168.  
  169. static void Help( const char *program )
  170. {
  171.    printf("%s options:\n", program);
  172.    printf("  +/-dither      enable/disable dithering\n");
  173.    printf("  +/-depth       enable/disable depth test\n");
  174.    printf("  +flat          flat shading\n");
  175.    printf("  +smooth        smooth shading\n");
  176. }
  177.  
  178.  
  179. int main( int argc, char *argv[] )
  180. {
  181.    printf("For options:  %s -help\n", argv[0]);
  182.    glutInit( &argc, argv );
  183.    glutInitWindowSize( (int) Width, (int) Height );
  184.    glutInitWindowPosition( 0, 0 );
  185.  
  186.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  187.  
  188.    glutCreateWindow( argv[0] );
  189.  
  190.    if (argc==2 && strcmp(argv[1],"-help")==0) {
  191.       Help(argv[0]);
  192.       return 0;
  193.    }
  194.  
  195.    Init( argc, argv );
  196.  
  197.    glutReshapeFunc( Reshape );
  198.    glutKeyboardFunc( Key );
  199.    glutDisplayFunc( Display );
  200.    glutIdleFunc( Idle );
  201.  
  202.    glutMainLoop();
  203. }
  204.